package com.net.wanjian.networkhospitalmanager.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.net.wanjian.networkhospitalmanager.MyApplication;
import com.net.wanjian.networkhospitalmanager.R;
import com.net.wanjian.networkhospitalmanager.adapter.base.RecyclerBaseAdapter;
import com.net.wanjian.networkhospitalmanager.bean.DeviceOperationListResult;
import com.net.wanjian.networkhospitalmanager.net.HttpUtil;
import com.net.wanjian.networkhospitalmanager.utils.PicassoUtil;
import com.net.wanjian.networkhospitalmanager.utils.SharedXmlUtil;
import com.net.wanjian.networkhospitalmanager.utils.TimeDateUtils;
import com.net.wanjian.networkhospitalmanager.utils.URLDecoderUtil;
import com.zhy.autolayout.utils.AutoUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Description:
 * Created by lidongcai on 17/4/27.
 * Email:  lidongcaiocc@163.com
 */

public class LaboratoryListAdapter extends RecyclerBaseAdapter<DeviceOperationListResult.DataBean.LabReserveMemberOperationListBean, LaboratoryListAdapter.ViewHolder> {

    public LaboratoryListAdapter(Context context) {
        super(context);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_lab_list, parent, false);
        AutoUtils.auto(view);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, DeviceOperationListResult.DataBean.LabReserveMemberOperationListBean item, int position) {
        holder.setItemLabListDeviceIv(item.getDeviceID());
        holder.setItemLabListIngTv(item.getOperatioinState());
        holder.setItemLabListNameTv(item.getDeviceName());
        holder.setItemLabListPhotoIv(item.getIsExistOperationPhoto());
        holder.setItemLabListTextIv(item.getIsExistOperationExperience());
        //这里为使用时间，应该通过 操作结束时间 － 操作开始时间
        holder.setItemLabListTimeTv(item.getOperatioinState(),item.getLabReserveMemberOperationStart(),item.getLabReserveMemberOperationEnd());
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_lab_list_device_iv)
        ImageView itemLabListDeviceIv;
        @BindView(R.id.item_lab_list_name_tv)
        TextView itemLabListNameTv;
        @BindView(R.id.item_lab_list_time_tv)
        TextView itemLabListTimeTv;
        @BindView(R.id.item_lab_list_ing_tv)
        TextView itemLabListIngTv;
        @BindView(R.id.item_lab_list_photo_iv)
        ImageView itemLabListPhotoIv;
        @BindView(R.id.item_lab_list_text_iv)
        ImageView itemLabListTextIv;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        //使用id来加载图片
        public void setItemLabListDeviceIv(String deviceID) {
            PicassoUtil.loadImage(MyApplication.getContext(),HttpUtil.getDevicePhoto(URLDecoderUtil.getDecoder(deviceID), SharedXmlUtil.getInstance().getHospitalId()),R.mipmap.user,itemLabListDeviceIv);
        }
        //设置名字
        public void setItemLabListNameTv(String deviceName) {
            this.itemLabListNameTv.setText(URLDecoderUtil.getDecoder(deviceName));
        }

        //使用时间
        //这里为使用时间，应该通过 操作结束时间 － 操作开始时间
        public void setItemLabListTimeTv(String ing,String startTime,String endTime) {
            if (URLDecoderUtil.getDecoder(ing).equals("0")) {
//                this.itemLabListIngTv.setText("正在进行");
//                long start = TimeDateUtils.getMillion(TimeDateUtils.formatTime(URLDecoderUtil.getDecoder(startTime)));
//                String ingTime = TimeDateUtils.getYearMonthDayHourMinuteSecond(System.currentTimeMillis() - start).split(" ")[1];
                this.itemLabListTimeTv.setText("使用时间："+TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(startTime)));
            }else {
//                this.itemLabListIngTv.setText("已完成");
//                long start = TimeDateUtils.getMillion(TimeDateUtils.formatTime(URLDecoderUtil.getDecoder(startTime)));
//                long end = TimeDateUtils.getMillion(TimeDateUtils.formatTime(URLDecoderUtil.getDecoder(endTime)));
                this.itemLabListTimeTv.setText(TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(startTime))+"－"+TimeDateUtils.formatTimeFriend(URLDecoderUtil.getDecoder(endTime)));

            }

        }

        //进行的状态
        public void setItemLabListIngTv(String ing) {
            if (URLDecoderUtil.getDecoder(ing).equals("0")) {
                this.itemLabListIngTv.setText("正在进行");
            }else {
                this.itemLabListIngTv.setText("已完成");
            }
        }

        //是否有图
        public void setItemLabListPhotoIv(String operationPhoto) {
            if (URLDecoderUtil.getDecoder(operationPhoto).equals("0")){
                this.itemLabListPhotoIv.setVisibility(View.INVISIBLE);
            }else {
                this.itemLabListPhotoIv.setVisibility(View.VISIBLE);
            }
        }

        //是否有文字
        public void setItemLabListTextIv(String experience) {
            if (URLDecoderUtil.getDecoder(experience).equals("0")){
                this.itemLabListTextIv.setVisibility(View.INVISIBLE);
            }else {
                this.itemLabListTextIv.setVisibility(View.VISIBLE);
            }
        }
    }

}
